#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <unistd.h>
std::mutex mtx;
std::condition_variable cv;
bool ready = false;
void thread1(){
    std::unique_lock<std::mutex> lock(mtx);
    printf("thread %d get lock!\n",std::this_thread::get_id());
    printf("thread %d release lock!\n",std::this_thread::get_id());
    cv.wait(lock,[](){
        return ready;
    });   // 由于ready = flase，cv.wait进入堵塞，会将lock释放，并等待cv.notify_one();
            //  满足条件后再次获得锁
    printf("thread %d get lock!\n",std::this_thread::get_id());
    printf("thread %d release lock\n",std::this_thread::get_id());
    cv.notify_one();
}
void thread2(){
    sleep(2);
    std::lock_guard<std::mutex> lock(mtx);
    printf("thread2 get lock!\n");
    ready = true;
    printf("awake thread1\n");
    cv.notify_one();
    printf("thread2 release lock\n");
}

int main(){
    std::thread wait1(&thread1);
    std::thread wait2(&thread1);
    std::thread wait3(&thread1);
    std::thread t2(&thread2);
    wait1.join();
    wait2.join();
    wait3.join();
    t2.join();
}